home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 729 / ff / source / lists.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  3KB  |  134 lines

  1.  
  2. /*************************************************************************/
  3. /*                 Lists.c                 */
  4. /*                                     */
  5. /* This file contains linked-list handling functions that provide an     */
  6. /* interface between the needs of FileFind, and the Exec Node/List     */
  7. /* system                                 */
  8. /*                                     */
  9. /* This source file is Copyright 1992 by Dave Schreiber.  All Rights     */
  10. /* Reserved.                                 */
  11. /*************************************************************************/
  12.  
  13.  
  14. #include <exec/types.h>
  15. #include <exec/lists.h>
  16. #include <exec/exec.h>
  17. #include "Lists.h"
  18.  
  19.  
  20. /*Initialize a list*/
  21. BOOL initList(struct List **listHead)
  22. {
  23.    /*If the head of the lists exists, free it*/
  24.    if(*listHead!=NULL)
  25.       FreeMem(*listHead,sizeof(struct List));
  26.  
  27.    /*Allocate the head of the list*/
  28.    *listHead=(struct List *)AllocMem(sizeof(struct List),MEMF_CLEAR);
  29.  
  30.    /*Initialize the list if the allocation was successful*/
  31.    if(*listHead!=NULL)
  32.       NewList(*listHead);
  33.  
  34.    return(listHead!=NULL);
  35. }
  36.  
  37. /*Add a name to the list*/
  38. BOOL addItem(struct List *listHead,char *name,char *prefixName,BOOL fileOnly)
  39. {
  40.    dosNameNode *nameNode;
  41.    char *nameOnly;
  42.    ULONG nodeSize;
  43.  
  44.    nodeSize=strlen(name)+sizeof(dosNameNode)+1;
  45.  
  46.    /*Allocate enough memory for the node*/
  47.    if((nameNode=(dosNameNode *)AllocMem(nodeSize,MEMF_CLEAR))!=NULL)
  48.    {
  49.       /*Copy the filename into the node's buffer*/
  50.       strcpy(nameNode->fullFilename,name);
  51.  
  52.       /*Store a pointer to the filename (or whole name)*/
  53.       if(fileOnly)
  54.      nameOnly=(char *)FilePart(nameNode->fullFilename);
  55.       else
  56.      nameOnly=nameNode->fullFilename;
  57.  
  58.       if(prefixName!=NULL)
  59.       {
  60.      if((nameNode->node.ln_Name=(char *)
  61.            AllocMem(strlen(prefixName)+1,0L))!=NULL)
  62.         strcpy(nameNode->node.ln_Name,prefixName);
  63.       }
  64.       else
  65.      if((nameNode->node.ln_Name=(char *)AllocMem(strlen(nameOnly)+1,0L))!=NULL)
  66.         strcpy(nameNode->node.ln_Name,nameOnly);
  67.  
  68.       if(nameNode->node.ln_Name==NULL)
  69.       {
  70.      FreeMem(nameNode,nodeSize);
  71.      nameNode=NULL;
  72.       }
  73.  
  74.  
  75.    }
  76.    if(nameNode!=NULL)
  77.    {
  78.       /*Other initializations*/
  79.       nameNode->node.ln_Type=NAMENODE_ID;
  80.       nameNode->node.ln_Pri=0;
  81.  
  82.       /*Add to the end of the list*/
  83.       AddTail(listHead,(struct Node *)nameNode);
  84.       return(TRUE);
  85.    }
  86.    else
  87.       return(FALSE);
  88. }
  89.  
  90. /*Get the item at ordinal number 'nodeNum'*/
  91. dosNameNode *getItem(struct List *listHead,ULONG nodeNum)
  92. {
  93.    dosNameNode *node;
  94.  
  95.    /*Return NULL if the list is empty*/
  96.    if(listHead->lh_Head->ln_Succ==NULL)
  97.       return(NULL);
  98.  
  99.    /*Otherwise, find the appropriate node*/
  100.    for(node=(dosNameNode *)listHead->lh_Head; node->node.ln_Succ && nodeNum!=0;
  101.     node=(dosNameNode *)node->node.ln_Succ,nodeNum--);
  102.  
  103.    return(node);
  104. }
  105.  
  106. /*Free a linked list*/
  107. void freeList(struct List **listHead)
  108. {
  109.    dosNameNode *curNode;
  110.    dosNameNode *nextNode;
  111.  
  112.    /*Get the head of the list*/
  113.    curNode=(dosNameNode *)((*listHead)->lh_Head);
  114.  
  115.    /*Loop while there are still nodes*/
  116.    while((nextNode=(dosNameNode *)curNode->node.ln_Succ)!=NULL)
  117.    {
  118.       /*Free a node's memory*/
  119.       FreeMem(curNode->node.ln_Name,strlen(curNode->node.ln_Name)+1);
  120.       FreeMem(curNode,sizeof(dosNameNode)+strlen(curNode->fullFilename)+1);
  121.       curNode=nextNode;
  122.    }
  123.  
  124.    /*Free the head of the list*/
  125.    FreeMem(*listHead,sizeof(struct List));
  126.    *listHead=NULL;
  127.  
  128.    return;
  129. }
  130.  
  131. /*End of Lists.c*/
  132.  
  133.  
  134.